package com.jdl.iscm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import com.jdl.iscm.entity.excel.NrtExcel;
import lombok.Data;
import org.testng.collections.Maps;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author: Ran
 * @Date: 2021/8/23
 **/
@Data
public class NrtListener extends AnalysisEventListener<NrtExcel> {

    private final List<NrtExcel> nrtExcelList = Lists.newArrayList();

    @Override
    public void invoke(NrtExcel nrtExcel, AnalysisContext analysisContext) {

        nrtExcelList.add(nrtExcel);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        nrtExcelList.stream().map(nrtExcel -> {
            //用于设置维度 & 场景 & 下单日的处理
            Map<String, String> dimensionMap = Maps.newLinkedHashMap();
            List<String> sceneList = Lists.newArrayList();
            List<String> orderdayList = Lists.newArrayList();
            String[] strArr1 = nrtExcel.getDimensions().split(",");
            IntStream.range(0, strArr1.length).forEach(x -> {
                String[] dimension = strArr1[x].split("=");
                dimensionMap.put(dimension[0], dimension[1]);
            });
            String[] strArr2 = nrtExcel.getScenes().split(",");
            IntStream.range(0, strArr2.length).forEach(x -> sceneList.add(strArr2[x]));
            String[] strArr3 = nrtExcel.getNrtOrderDays().split(",");
            IntStream.range(0, strArr3.length).forEach(x -> orderdayList.add(strArr3[x]));
            nrtExcel.setDimensionMap(dimensionMap);
            nrtExcel.setSceneList(sceneList);
            nrtExcel.setNrtOrderDayList(orderdayList);
            return nrtExcel;
        }).collect(Collectors.toList());
    }
}
